package com.wealth.coupon.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
 * 生码工具类
 * create  by  zhang
 * modify by  chan on 2016-09-09 删除不用的方法
 */
public class LabelFileUtil {
	
	 /** 
	  * 创建文件 
	  * @param fileName 
	  * @return 
	  */  
	 public static boolean createFile(File fileName) throws Exception{  
		if (!fileName.exists()) {
			fileName.getParentFile().mkdirs();
			fileName.createNewFile();
			return true;
		}
		return false;
	 }  
	 
	/**
	 * 生成UTF-8的文件
	 * @param dataBuf
	 * @param filePath
	 * @return
	 */
	public static boolean saveToFile(String dataBuf, String filePath){
		if(dataBuf==null || filePath==null)
			return false;
		boolean ret = false;
		OutputStreamWriter out = null;
		try {
			ret = createFile(new File(filePath));
			if(ret){
				out = new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8");
				out.write(dataBuf);
				ret = true;
			}
			return ret;
		}
		catch (Exception e){
			e.printStackTrace();
		}
		finally{
			if (out != null){
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				out = null;
			}
		}
		
		return ret;

	}
	
	/**
	 * 文件添加内容
	 * @param dataBuf
	 * @param filePath
	 * @param append
	 * @return
	 */
	public static boolean saveToFileEx(byte[] dataBuf, String filePath, boolean append){
		if(dataBuf==null || filePath==null)
			return false;
		
		boolean ret = false;
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath,append);
			out.write(dataBuf);
			ret = true;
		}
		catch (Exception e){
			
		}
		finally{
			if (out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				out = null;
			}
		}
		
		return ret;

	}
	
	
}
